package com.zsp.coupon.vo;


import com.zsp.coupon.constant.CouponStatus;
import com.zsp.coupon.constant.PeriodType;
import com.zsp.coupon.entity.Coupon;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang.time.DateUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class CouponClassify {
    //可用使用的
    private List<Coupon> uable;
    //已经使用了的
    private List<Coupon> used;
    // 已经过期的
    private  List<Coupon> expired;


//对其当前的优惠卷进行分类
    public  static  CouponClassify classify(List<Coupon> coupons){
        List<Coupon> usable  = new ArrayList<>(coupons.size());
        List<Coupon> used = new ArrayList<>(coupons.size());
        List<Coupon> expried = new ArrayList<>(coupons.size());

        coupons.forEach(c -> {
            boolean isTimeExpired;
            long curTime = new Date().getTime();

            if (c.getTemplateSDK().getRule().getException().getPeriod().equals(
                    PeriodType.REGULAR.getCode()
            )){
                isTimeExpired = c.getTemplateSDK()
                        .getRule().
                        getException()
                        .getDeadline() <= curTime;
            }else {
                isTimeExpired = DateUtils.addDays(
                        c.getAssignTiem(),
                        c.getTemplateSDK().getRule()
                                .getException().getGap())
                        .getTime() <=curTime;
            }

            if (c.getStatus() == CouponStatus.USED) {
                used.add(c);
            }else if (c.getStatus() == CouponStatus.EXPIRED || isTimeExpired){
                    expried.add(c);
            } else {
                usable.add(c);
            }
        });
        return new  CouponClassify(usable,used,expried);
    }
}
